//
//  newsViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "newsViewController.h"
#import "newestOtherCell.h"
#import "newestCell.h"
#import "newsModel.h"
#import "newsDetailViewController.h"
@interface newsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *newsTableV;
@property(nonatomic, retain)NSMutableArray *newsArr;
@property(nonatomic, retain)MBProgressHUD *HUB;
@property(nonatomic, assign)NSInteger nextPage;//记录下一页
@property(nonatomic, assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉
@property(nonatomic, retain)NSMutableArray *images;
@end

@implementation newsViewController

-(void)dealloc{
    [_images release];
    [_HUB release];
    [_newsArr release];
    [_newsTableV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUB.mode = MBProgressHUDModeIndeterminate;//不同类型
    [self.HUB show:YES];
    self.HUB.labelText = @"loading...";
    self.isUpLoading = NO;//进入界面下拉
    // Do any additional setup after loading the view.
    self.newsArr = [NSMutableArray array];
    
    
    
    self.newsTableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.newsTableV.backgroundColor = [UIColor clearColor];
    self.newsTableV.delegate = self;
    self.newsTableV.dataSource = self;
    [self.view addSubview:self.newsTableV];
    [self.newsTableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    self.newsTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_newsTableV release];
    
    
    [self.newsTableV registerClass:[newestCell class] forCellReuseIdentifier:@"newest"];
    [self.newsTableV registerClass:[newestOtherCell class] forCellReuseIdentifier:@"newestOther"];
    
    //上下拉图片数组
    self.images = [NSMutableArray array];
    for (int i = 1; i < 4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%d.png",i]];
        [self.images addObject:image];
    }
    [self addHeader];
    [self addFooter];
    [self.newsTableV.mj_header beginRefreshing];
}

//下拉刷新
-(void)addHeader{
    __block newsViewController *newVC = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        // newVC.nextPage = 1;//记录页码
        newVC.isUpLoading = NO;//标记下拉操作
        [newVC getDataWith:@"http://app.api.autohome.com.cn/autov5.0.0/news/newslist-pm1-c0-nt1-p1-s30-l0.json"];//每次下拉重新请求数据
    }];
    [header setImages:self.images forState:MJRefreshStateIdle];
    [header setImages:self.images forState:MJRefreshStatePulling];
    [header setImages:self.images forState:MJRefreshStateRefreshing];
    self.newsTableV.mj_header = header;
}

//上拉加载
-(void)addFooter{
    __block newsViewController *newVC = self;
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        //newVC.nextPage ++;//上拉一次自增一次
        newVC.isUpLoading = YES;//标记为上拉操作
        newsModel *model = [self.newsArr lastObject];
        NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/news/newslist-pm2-c0-nt1-p2-s20-l%@.json",model.lasttime];
        [newVC getDataWith:str];//每次上拉请求数据
    }];
    [footer setImages:self.images forState:MJRefreshStateRefreshing];
    self.newsTableV.mj_footer = footer;
}


-(void)getDataWith:(NSString *)str{
    [ZYNetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        NSDictionary *result = [responseObject objectForKey:@"result"];
        NSMutableArray *listArr = [result objectForKey:@"newslist"];
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.newsArr  removeAllObjects];
        }
        for (NSDictionary *dic in listArr) {
            newsModel *model = [[newsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.newsArr addObject:model];
            NSLog(@"%ld",(long)model.myId);
            [model release];
        }
        [self.newsTableV.mj_header endRefreshing];
        [self.newsTableV.mj_footer endRefreshing];
        [self.newsTableV reloadData];
        [self.HUB hide:YES];//菊花条
    } filed:^(NSError *error) {
        
        
    }];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    newsModel *model = [self.newsArr objectAtIndex:indexPath.row];
    newsDetailViewController *VC = [[newsDetailViewController  alloc] init];
    VC.myId = [NSString stringWithFormat:@"%ld",(long)model.myId];
    [self.navigationController pushViewController:VC animated:YES];
    [VC release];
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    newestModel *model = [self.newsArr objectAtIndex:indexPath.row];
    if (model.mediatype == 10) {
        return 165;
    }else{
        return 130;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    newsModel *model = [self.newsArr objectAtIndex:indexPath.row];
    if (model.mediatype == 10) {
        newestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newestOther" forIndexPath:indexPath];
        cell.Model = model;
        return cell;
    }else{
        newestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newest" forIndexPath:indexPath];
        cell.Model = model;
        return cell;
    }
}
    
    
    
    
    
    
    
    







@end
